草庐IT

javascript oop、instanceof 和基类

全部标签

javascript - `this instanceof String` 和 `"foo"instanceof String` 有什么区别?

我正在像这样扩展对象:Object.prototype.is_a=function(x){returnthisinstanceofx;}一切正常"foo".is_a(String)//true"foo".is_a(Object)//true"foo".is_a(Array)//false"foo".is_a(Function)//false"foo".is_a(Boolean)//false"foo".is_a(Date)//false"foo".is_a(Number)//false"foo".is_a(RegExp)//false但是,当"foo"instanceofString/

javascript oop、instanceof 和基类

我正在用JavaScript设计一些类层次结构。到目前为止它工作正常,但我看不到如何确定一个对象是否是父类的“实例”。示例:functionBaseObject(name){this.name=name;this.sayWhoAmI=function(){console.log(this.name+'isaDerivation1:'+(thisinstanceofDerivation1));console.log(this.name+'isaDerivation2:'+(thisinstanceofDerivation2));console.log(this.name+'isaBase

javascript - 在 javascript 中使用 instanceof 运算符是性能问题吗?

关闭。这个问题需要更多focused.它目前不接受答案。想改进这个问题吗?更新问题,使其只关注一个问题editingthispost.关闭7年前。Improvethisquestion我听说java中的instanceof运算符是一个性能问题,对于Javascript(IE6、IE7、IE8、FF、Chrome、safari等)也是如此吗?任何指向真实论文的链接都会有所帮助。

javascript - instanceof 和 constructor 属性之间的区别

ainstanceofb是否完全等同于a.constructor===b?如果不是,两者有什么区别? 最佳答案 没有。instanceof还检查​​“继承的”构造函数。有关详细信息,请参阅规范。(here和here) 关于javascript-instanceof和constructor属性之间的区别,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/18172902/

javascript - 此 instanceof arguments.callee 的严格模式替代方案

有一个老技巧(thatIlearnedonSO)可以捕捉到将构造函数作为函数调用,即忘记new关键字。以下内容或类似内容位于顶部的每个构造函数中。if(!(thisinstanceofarguments.callee)){throwError("Constructorcalledasafunction");}当您需要"usestrict";时有哪些替代方案?能否保留其通用性?或者我们是否必须使用构造函数的名称来代替arguments.callee? 最佳答案 arguments.callee本身已被弃用,取而代之的是命名函数表达式。

javascript - 覆盖基类的特权方法

我怎样才能让子类覆盖基类的特权方法?如果不可能,是否有另一种方法可以实现我在下面的简单代码示例中尝试实现的目标?我无法将基类函数parseXML()转换为公共(public)函数,因为它需要访问私有(private)变量functionBaseClass(){varmap={};//IcannotmakethisfunctionpublicBECAUSEitaccesses&changesprivatevariablesthis.parseXML=function(key,value){alert("BaseClass::parseXML()");map[key]=value;}}fu

javascript - instanceof 自定义错误类返回 false

这个问题在这里已经有了答案:Extendingbuilt-innativesinES6withBabel(3个答案)关闭5年前。为什么这会导致false?'usestrict';classInvalidCredentialsErrorextendsError{constructor(msg){super(msg);this.name='InvalidCredentialsError';}}consterr=newInvalidCredentialsError('');console.log(errinstanceofInvalidCredentialsError);但这会返回true:

Javascript 继承 - instanceof 不工作?

我正在使用javascript和html5编写一个简单的平台游戏。我以面向对象的方式使用javascript。为了让继承工作,我使用了以下内容;//http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/functioncopyPrototype(descendant,parent){varsConstructor=parent.toString();varaMatch=sConstructor.match(/\s*function(.*)\(/);if(aMatch!=null){descendant.prot

javascript - 使用 'instanceof function() {}' 的原因?

在Mozilla开发者中心,有一个关于Function.prototype.bind的页面功能,并为不支持该功能的浏览器提供兼容功能。但是,在分析这个兼容性代码时,我无法找出他们为什么使用instanceofnop。nop已设置为function(){}。这对应于bindECMA规范的哪一部分?哪些变量是function(){}的实例?下面返回false,所以完全不知道是干什么用的。在执行instanceoffunction(){}检查时,哪些事情会返回true?(function(){})instanceof(function(){})//false代码如下:Function.pro

javascript - 这个 instanceof 错误消息是什么意思?

我在Chrome中尝试使用instanceof,但收到一条错误消息。我认为我知道为什么(你必须在instanceof关键字之后提供一个函数,它是创建对象的构造函数),但错误消息似乎在说明一些事情否则:[1,2,3]instanceofArray//true[1,2,3]instanceof[]//TypeError:Expectingafunctionininstanceofcheck,butgot1,2,3这是否意味着我应该用函数替换[1,2,3]?我会认为[1,2,3]是正确的并且[]是问题所在,应该用函数替换,但看起来错误消息是说反话。有人可以解释一下我是如何错误地解释错误消息的